package com.kylin.hiberbate;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model
 * @author MyEclipse - Hibernate Tools
 */
public class BaseHibernateDAO implements IBaseHibernateDAO {
	public Session getSession() {
		return HibernateSessionFactory.getSession();
	}
    
    protected void tranSave(Object transientInstance) {
        try {
            getSession().save(transientInstance);
         } catch (RuntimeException re) {
            throw re;
        }
    }
    
    protected void save(Object transientInstance) {
        try {
        	getSession().beginTransaction();
            getSession().save(transientInstance);
            getSession().getTransaction().commit();
         } catch (RuntimeException re) {
            throw re;
        }
    }
    
    protected void saveOrUpdate(Object transientInstance) {
        try {
        	getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            getSession().getTransaction().commit();
         } catch (RuntimeException re) {
            throw re;
        }
    }
    
    protected void update(Object transientInstance) {
        try {
        	getSession().beginTransaction();
            getSession().update(transientInstance);
            getSession().getTransaction().commit();
         } catch (RuntimeException re) {
            throw re;
        }
    }    
	protected void delete(Object persistentInstance) {
        try {
        	getSession().beginTransaction();
            getSession().delete(persistentInstance);
            getSession().getTransaction().commit();
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    protected Object findById(Class clz, java.lang.Integer id) {
        try {
            Object instance = getSession().get(clz, id);
            return instance;
        } catch (RuntimeException re) {
            throw re;
        }
    }
          
    public List findByExample(Object instance) {
       try {
            List results = getSession()
                    .createCriteria(instance.getClass().getName())
                    .add(Example.create(instance)).list();
            return results;
        } catch (RuntimeException re) {
            throw re;
        }
    }    
    	
    public Object merge(Object detachedInstance) {
        try {
        	Object result = getSession().merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            throw re;
        }
    }

    public void attachDirty(Object instance) {
        try {
            getSession().saveOrUpdate(instance);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    public void attachClean(Object instance) {
        try {
            getSession().lock(instance, LockMode.NONE);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
}